﻿using System;
using System.Collections.Generic;

namespace JinianNet.JNCms
{
    public static class Config
    {
        public static Dictionary<string, string> Read(string key, bool isNeedCache)
        {
            Dictionary<string, string> config = null;
            if (isNeedCache)
            {
                Caching.ICache cache = JinianNet.Web.Cache.Create(Web.Cache.CacheMode.Static);
                config = JinianNet.Web.Cache.Create(Web.Cache.CacheMode.Static).Get(string.Concat("Cache_Config_", key)) as Dictionary<string, string>;
            }
            if (config == null)
            {
                config = IO.Setting.Read(JinianNet.Common.Path.Concat(JinianNet.Web.HttpRunInfo.SitePath, string.Concat("Config/", key, ".config")));
            }
            if (isNeedCache)
            {
                JinianNet.Web.Cache.Create(Web.Cache.CacheMode.Static)[string.Concat("Cache_Config_", key)] = config;
            }
            return config;
        }

        public static void Write(Dictionary<string, string> config,string key)
        {
            Caching.ICache cache = JinianNet.Web.Cache.Create(Web.Cache.CacheMode.Static);
            cache.Remove(string.Concat("Cache_Config_", key));
            IO.Setting.Write(config,JinianNet.Common.Path.Concat(JinianNet.Web.HttpRunInfo.SitePath, string.Concat("Config/", key, ".config")));
        }

        public static Dictionary<string, string> PageConfig
        {
            get
            {
                return Read("Page", true);
            }
        }
        public static Dictionary<string, string> EmailConfig
        {
            get
            {
                return Read("Email", true);
            }
        }
        public static Dictionary<string, string> SafeConfig
        {
            get
            {
                return Read("Safe", true);
            }
        }
        public static Dictionary<string, string> SiteConfig
        {
            get
            {
                return Read("Site", true);
            }
        }

        /// <summary>
        /// 分页配置
        /// </summary>
        public class Page
        {
            public static int Size
            {
                get { return int.Parse(PageConfig["Size"]); }
            }
            public static string Style
            {
                get { return PageConfig["Size"]; }
            }
        }

        /// <summary>
        /// 邮箱服务器配置
        /// </summary>
        public class Email
        {
            public static int Port
            {
                get { return int.Parse(EmailConfig["Port"]); }
            }
            public static string Smtp
            {
                get { return EmailConfig["Smtp"]; }
            }
            public static string Mail
            {
                get { return EmailConfig["Mail"]; }
            }
            public static string User
            {
                get { return EmailConfig["User"]; }
            }
            public static string Password
            {
                get { return EmailConfig["Password"]; }
            }
            public static bool EnableSsl
            {
                get { return EmailConfig["EnableSsl"].Equals("True", StringComparison.OrdinalIgnoreCase); }
            }
        }

        /// <summary>
        /// 网站安全配置
        /// </summary>
        public class Safe
        {
            /// <summary>
            /// 密钥KEY
            /// </summary>
            public static string DESKEY
            {
                get { return SafeConfig["DESKEY"]; }
            }
            /// <summary>
            /// 密钥IV
            /// </summary>
            public static string DESIV
            {
                get { return SafeConfig["DESIV"]; }
            }
        }

        /// <summary>
        /// 网站基本配置
        /// </summary>
        public class Site
        {
            public static string Title { get { return SiteConfig["Title"]; } }
            public static string KeyWords { get { return SiteConfig["KeyWords"]; } }
            public static string Description { get { return SiteConfig["Description"]; } }
            public static string TemplateDirectory { get { return SiteConfig["TemplateDirectory"]; } }
            public static string SiteDirectory { get { return JinianNet.Web.HttpRunInfo.SiteVirtualPath; } }
            public static string Theme { get { return SiteConfig["Theme"]; } }
            public static string Logo { get { return SiteConfig["Logo"]; } }
            public static string Host { get { return SiteConfig["Host"]; } }
            public static string Cookie { get { return SiteConfig["Cookie"]; } }
            public static string Name { get { return SiteConfig["Name"]; } }
            public static bool IsUrlRewrite
            {
                get { return SiteConfig["IsUrlRewrite"].Equals("True", StringComparison.OrdinalIgnoreCase); }
            }
            public static string RewriteSuffix { get { return SiteConfig["RewriteSuffix"]; } }
            public static string TempLateSuffix { get { return SiteConfig["TempLateSuffix"]; } }
            public static string HtmlSuffix { get { return SiteConfig["HtmlSuffix"]; } }
            public static bool CreateHtml
            {
                get { return SiteConfig["CreateHtml"].Equals("True", StringComparison.OrdinalIgnoreCase); }
            }
            public static string Webmaster { get { return SiteConfig["Webmaster"]; } }
            public static string Email { get { return SiteConfig["Email"]; } }
            public static string Copyright { get { return SiteConfig["Copyright"]; } }
            public static bool IsClose
            {
                get { return SiteConfig["IsClose"].Equals("True", StringComparison.OrdinalIgnoreCase); }
            }
            public static string CloseReason { get { return SiteConfig["CloseReason"]; } }
            public static string BadWords { get { return SiteConfig["BadWords"]; } }
            public static string BadWordsReplacement { get { return SiteConfig[BadWordsReplacement]; } }
            public static string UserNameLengthMin { get { return SiteConfig["UserNameLengthMin"]; } }
            public static string UserNameLengthMax { get { return SiteConfig["UserNameLengthMax"]; } }
            //public static string EnableUserSpace { get { return SiteConfig["EnableUserSpace"]; } }
            public static string ShowSexyInfoInProfile { get { return SiteConfig["ShowSexyInfoInProfile"]; } }
            public static string LoginNeedImgValidation { get { return SiteConfig["LoginNeedImgValidation"]; } }
            public static string RegisterNeedImgValidateion { get { return SiteConfig["RegisterNeedImgValidateion"]; } }
            public static string SystemMsgTitle { get { return SiteConfig["SystemMsgTitle"]; } }
            public static string SystemMsgContent { get { return SiteConfig["SystemMsgContent"]; } }
            public static string ReservedUserName { get { return SiteConfig["ReservedUserName"]; } }
            public static string ReservedUserUrl { get { return SiteConfig["ReservedUserUrl"]; } }
            public static string PhotoThumbWidth { get { return SiteConfig["PhotoThumbWidth"]; } }
            public static string PhotoThumbHeight { get { return SiteConfig["PhotoThumbHeight"]; } }
            public static string PhotoThumbWidthMedium { get { return SiteConfig["PhotoThumbWidthMedium"]; } }
            public static string PhotoThumbHeightMedium { get { return SiteConfig["PhotoThumbHeightMedium"]; } }
            public static string PhotoThumbWidthBig { get { return SiteConfig["PhotoThumbWidthBig"]; } }
            public static string PhotoThumbHeightBig { get { return SiteConfig["PhotoThumbHeightBig"]; } }
            public static string AvatarThumbWidth { get { return SiteConfig["AvatarThumbWidth"]; } }
            public static string AvatarThumbHeight { get { return SiteConfig["AvatarThumbHeight"]; } }
            public static string AvatarThumbWidthMedium { get { return SiteConfig["AvatarThumbWidthMedium"]; } }
            public static string AvatarThumbHeightMedium { get { return SiteConfig["AvatarThumbHeightMedium"]; } }
            public static string AvatarThumbWidthBig { get { return SiteConfig["AvatarThumbWidthBig"]; } }
            public static string AvatarThumbHeightBig { get { return SiteConfig["AvatarThumbHeightBig"]; } }
            public static string IsSaveAvatarMedium { get { return SiteConfig["IsSaveAvatarMedium"]; } }
            public static string IsSaveAvatarBig { get { return SiteConfig["IsSaveAvatarBig"]; } }
            public static string ReservedKey { get { return SiteConfig["ReservedKey"]; } }
            public static string NoRefreshSecond { get { return SiteConfig["NoRefreshSecond"]; } }
            public static string Spider { get { return SiteConfig["Spider"]; } }
            public static string MaxOnline { get { return SiteConfig["MaxOnline"]; } }
            public static string MaxOnlineTime { get { return SiteConfig["MaxOnlineTime"]; } }
        }


        public static string GetTips(string msg, string url, bool succeed)
        {
            string[] result = new string[7];
            result[0] = "<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>提示</title><style type=\"text/css\">body { font-size:12px; margin:0px;}#Box {width:400px; border:solid 1px #52B1E5;margin:50px auto auto auto;box-shadow:0 0 10px #666;}#menu {height:25px;line-height:25px; color:#fff; padding-left:20px; font-weight:bold; background-color:#69C3F5;filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#8AD3F8,endcolorstr=#69C3F5,gradientType=0);background:-moz-linear-gradient(top, #8AD3F8, #69C3F5);background:-webkit-gradient(linear, 0 0, 0 bottom, from(#8AD3F8), to(#69C3F5));background:-o-linear-gradient(top, #8AD3F8, #69C3F5); border-bottom:solid 1px #52B1E5; border-top:solid 1px #C7E5F6;}#Content { line-height:25px; width:380px; margin:10px auto 10px auto;}#Content table { border:0px #fff none; empty-cells:hide; width:100%; padding:0px;}#Content table tr td { vertical-align:top;}#Content .left { width:100px; float:left; clear:left; text-align:center; line-height:100px; font-size:24px; font-weight:bold; overflow:hidden; text-align:center; }#Content .right { width:270px; float:right; clear:right; text-align:left;}.cls { clear:both;}#Content .error {color:#FF3333; font-size:120px;}#Content .proper { color:#669966; font-size:80px; font-weight:bold;}</style></head><body><div id=\"Box\"><div id=\"menu\">信息提示</div><div id=\"Content\">";
            if (!succeed)
            {
                result[1] = "<div class=\"left error\">&times;</div><div class=\"right\"><strong>操作失败信息</strong>：<br />";
                result[2] = msg;
                result[3] = "<br /><span class=\"red\">抱歉！操作失败</span>";
            }
            else
            {
                result[1] = "<div class=\"left proper\">&radic;</div><div class=\"right\"><strong>操作结果</strong>：<br />";
                result[2] = msg;
                result[3] = "<br /><span class=\"red\">恭喜！操作成功</span>";
            }
            if (string.IsNullOrEmpty(url))
            {
                url = "javascript:history.back();";
            }
            result[4] = "<br/><a href=\"";
            result[5] = url;
            result[6] = "\">点击此处返回上一页</a></div><div class=\"cls\"></div></div></div></body></html>";

            return string.Concat(result);
        }

    }
}
